﻿using System.Diagnostics;
using Microsoft.Win32;

namespace JavascriptWebResourcesManager
{
    /// <summary>
    /// Class that manages file edition
    /// </summary>
    class EditionManager
    {
        /// <summary>
        /// Visual Studio program location registry key
        /// </summary>
        private string visualStudio2008Path = null;
        private string visualStudio2010Path = null;

        /// <summary>
        /// JavaScript file default editor location registry key
        /// </summary>
        private string defaultJsEditorRegistryKey = null;

        /// <summary>
        /// Indicates if Visual Studio 2008 is installed on the local computer
        /// </summary>
        private bool visualStudio2008Exists = false;

        /// <summary>
        /// Indicates if Visual Studio 2010 is installed on the local computer
        /// </summary>
        private bool visualStudio2010Exists = false;

        /// <summary>
        /// Gets a boolean that indicates if Visual Studio 2008 is installed on the local computer
        /// </summary>
        public bool VisualStudio2008Exists
        {
            get { return visualStudio2008Exists; }
        }

        /// <summary>
        /// Gets a boolean that indicates if Visual Studio is installed on the local computer
        /// </summary>
        public bool VisualStudio2010Exists
        {
            get { return visualStudio2010Exists; }
        }

        /// <summary>
        /// Initializes a new instance of the class EditionManager
        /// </summary>
        public EditionManager()
        {
            try
            {
                //using (RegistryKey key = Registry.LocalMachine)
                //{
                //    using (RegistryKey key2 = key.OpenSubKey(@"SOFTWARE\Classes\Applications\devenv.exe\shell\open\command", false))
                //    {
                //        visualStudioRegistryKey = (string)key2.GetValue(null);
                //    }
                //}

                using (RegistryKey key = Registry.ClassesRoot)
                {
                    using (RegistryKey key2 = key.OpenSubKey(@"Local Settings\SOFTWARE\Microsoft\Windows\Shell\MuiCache", false))
                    {
                        if (key2 != null)
                        {
                            foreach (string name in key2.GetValueNames())
                            {
                                if (name.IndexOf(@"Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe") > 0)
                                {
                                    visualStudio2010Path = name;
                                    visualStudio2010Exists = true;
                                    break;
                                }
                            }
                        }

                        //visualStudioRegistryKey = (string)key2.GetValue(null);
                    }
                }

                using (RegistryKey key = Registry.ClassesRoot)
                {
                    using (RegistryKey key2 = key.OpenSubKey(@"Local Settings\SOFTWARE\Microsoft\Windows\Shell\MuiCache", false))
                    {
                        if (key2 != null)
                        {
                            foreach (string name in key2.GetValueNames())
                            {
                                if (name.IndexOf(@"Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe") > 0)
                                {
                                    visualStudio2008Path = name;
                                    visualStudio2008Exists = true;
                                    break;
                                }
                            }
                        }

                       // visualStudioRegistryKey = (string)key2.GetValue(null);
                    }
                }

                using (RegistryKey key = Registry.ClassesRoot)
                {
                    using (RegistryKey key2 = key.OpenSubKey(@"JSFile\Shell\Edit\Command", false))
                    {
                        defaultJsEditorRegistryKey = (string)key2.GetValue(null);
                    }
                }

                //if (visualStudioRegistryKey != null)
                //{
                //    visualStudioExists = true;
                //}
            }
            catch
            {
                visualStudio2008Exists = false;
                visualStudio2010Exists = false;
            }
        }

        /// <summary>
        /// Launches Visual Studio
        /// </summary>
        /// <param name="fileName">File to edit</param>
        public void LaunchVisualStudio(string fileName)
        {
            //ProcessStartInfo startInfo = new ProcessStartInfo();
            //startInfo.FileName = visualStudioRegistryKey.Replace("\"%1\"", "");
            //startInfo.Arguments = "\"" + fileName + "\"";
            //startInfo.WindowStyle = ProcessWindowStyle.Normal;
            //startInfo.UseShellExecute = false;
            //Process.Start(startInfo);
        }

        /// <summary>
        /// Launches Visual Studio
        /// </summary>
        /// <param name="fileName">File to edit</param>
        public void LaunchVisualStudio2008(string fileName)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = visualStudio2008Path;
            startInfo.Arguments = "\"" + fileName + "\"";
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.UseShellExecute = false;
            Process.Start(startInfo);
        }

        /// <summary>
        /// Launches Visual Studio
        /// </summary>
        /// <param name="fileName">File to edit</param>
        public void LaunchVisualStudio2010(string fileName)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = visualStudio2010Path;
            startInfo.Arguments = "\"" + fileName + "\"";
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.UseShellExecute = false;
            Process.Start(startInfo);
        }

        /// <summary>
        /// Launches default JavaScript editor
        /// </summary>
        /// <param name="fileName">File to edit</param>
        public void LaunchDefaultTextEditor(string fileName)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = defaultJsEditorRegistryKey.Replace(" %1", "");
            startInfo.Arguments = "\"" + fileName + "\"";
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.UseShellExecute = false;
            Process.Start(startInfo);
        }
    }
}
